/**
 * Copyright [2011] [Raymond Pankhurst]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */
package passwordapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import javax.microedition.io.file.FileConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.OutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;

import javax.microedition.rms.*;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;
import passwordapp.FileBrowserExtended;

/**
 * @author rpankhurst
 */
public class MainMenu extends MIDlet implements CommandListener, PasswordFormListener {
    private RecordStore m_PasswordStore     = null;
    private CipherParameters m_CipherParams = null;
    private PasswordEntryForm m_PasswordForm  = null;
    private boolean midletPaused = false;
    private RecordVector m_Items   = new RecordVector();
    private String m_Msg = "";
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form frmLogin;
    private TextField txtPW;
    private TextField txtPW2;
    private List frmDBList;
    private Alert altConfigError;
    private Alert altConfirmExit;
    private Alert altConfirmRemove;
    private Form frmGetFileName;
    private TextField txtSaveFilename;
    private FileBrowserExtended fileBrowserExport;
    private FileBrowserExtended fileBrowserImport;
    private Command loginCommand;
    private Command itemCommand;
    private Command okCommand1;
    private Command screenCommand;
    private Command screenCommand1;
    private Command addCommand;
    private Command backCommand;
    private Command exitCommand;
    private Command okCommand2;
    private Command createCommand;
    private Command exitConfirmCommand;
    private Command removeCommand;
    private Command cancelCommand;
    private Command okCommand;
    private Command confirmRemoveCommand;
    private Command cancelRemoveCommand;
    private Command cancelChooseFile;
    private Command exportCommand;
    private Command selectFileCommand;
    private Command backCommand2;
    private Command itemCommand1;
    private Command backCommand1;
    private Command exportToDirCommand;
    private Command backCommand3;
    private Command itemCommand2;
    private Command importCommand;
//</editor-fold>//GEN-END:|fields|0|
    /**
     * The constructor.
     */
    public MainMenu() {
        
    }


//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here      
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        
        configLoad();//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
        
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmLogin ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of frmLogin component.
     * @return the initialized component instance
     */
    public Form getFrmLogin() {
        if (frmLogin == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            frmLogin = new Form("form", new Item[]{getTxtPW(), getTxtPW2()});//GEN-BEGIN:|14-getter|1|14-postInit
            frmLogin.addCommand(getExitCommand());
            frmLogin.addCommand(getLoginCommand());
            frmLogin.addCommand(getCreateCommand());
            frmLogin.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return frmLogin;
    }
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtPW ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of txtPW component.
     * @return the initialized component instance
     */
    public TextField getTxtPW() {
        if (txtPW == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            txtPW = new TextField("Enter password:", null, 32, TextField.ANY | TextField.PASSWORD);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return txtPW;
    }
//</editor-fold>//GEN-END:|16-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == altConfigError) {//GEN-BEGIN:|7-commandAction|1|51-preAction
            if (command == okCommand2) {//GEN-END:|7-commandAction|1|51-preAction
                // write pre-action user code here
                exitMIDlet();
//GEN-LINE:|7-commandAction|2|51-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|89-preAction
        } else if (displayable == altConfirmExit) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|3|89-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|4|89-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|5|91-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|91-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|110-preAction
        } else if (displayable == altConfirmRemove) {
            if (command == cancelRemoveCommand) {//GEN-END:|7-commandAction|7|110-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|8|110-postAction
                // write post-action user code here
            } else if (command == confirmRemoveCommand) {//GEN-LINE:|7-commandAction|9|112-preAction
                // write pre-action user code here
                RecordEntry ent = (RecordEntry) m_Items.elementAt(getFrmDBList().getSelectedIndex());
                m_Items.removeElementAt(getFrmDBList().getSelectedIndex());
                try {
                    m_PasswordStore.deleteRecord(ent.id);
                    switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|10|112-postAction
                } catch(Exception e) {
                    errorMessage("Can't delete record: " + e.toString(), null);
                }
                populateList();
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|175-preAction
        } else if (displayable == fileBrowserExport) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|11|175-preAction
                // write pre-action user code here
                fileBrowserExportAction();//GEN-LINE:|7-commandAction|12|175-postAction
                // write post-action user code here
            } else if (command == backCommand1) {//GEN-LINE:|7-commandAction|13|180-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|14|180-postAction
                // write post-action user code here
            } else if (command == exportToDirCommand) {//GEN-LINE:|7-commandAction|15|178-preAction
                // write pre-action user code here
                if(exportFileAction(getFileBrowserExport().getCurURL() + getTxtSaveFilename().getString())) {
                    switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|16|178-postAction
                } else {// write post-action user code here
                    errorMessage(m_Msg, null);
                }
            }//GEN-BEGIN:|7-commandAction|17|184-preAction
        } else if (displayable == fileBrowserImport) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|17|184-preAction
                // write pre-action user code here
                fileBrowserImportAction();//GEN-LINE:|7-commandAction|18|184-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|19|186-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|20|186-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|35-preAction
        } else if (displayable == frmDBList) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|21|35-preAction
                // write pre-action user code here
                frmDBListAction();//GEN-LINE:|7-commandAction|22|35-postAction
                // write post-action user code here
            } else if (command == addCommand) {//GEN-LINE:|7-commandAction|23|40-preAction
                // write pre-action user code here
                PasswordEntry ent = new PasswordEntry();
                getPasswordForm().setPasswordEntry(ent);
                getPasswordForm().show();
//GEN-LINE:|7-commandAction|24|40-postAction
                // write post-action user code here
            } else if (command == exitConfirmCommand) {//GEN-LINE:|7-commandAction|25|86-preAction
                // write pre-action user code here
                switchDisplayable(getAltConfirmExit(), getFrmDBList());//GEN-LINE:|7-commandAction|26|86-postAction
                // write post-action user code here
            } else if (command == exportCommand) {//GEN-LINE:|7-commandAction|27|123-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmGetFileName());//GEN-LINE:|7-commandAction|28|123-postAction
                // write post-action user code here
            } else if (command == importCommand) {//GEN-LINE:|7-commandAction|29|165-preAction
                // write pre-action user code here
                switchDisplayable(null, getFileBrowserImport());//GEN-LINE:|7-commandAction|30|165-postAction
                // write post-action user code here
            } else if (command == removeCommand) {//GEN-LINE:|7-commandAction|31|84-preAction
                // write pre-action user code here
                switchDisplayable(getAltConfirmRemove(), getFrmDBList());//GEN-LINE:|7-commandAction|32|84-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|33|156-preAction
        } else if (displayable == frmGetFileName) {
            if (command == backCommand2) {//GEN-END:|7-commandAction|33|156-preAction
                // write pre-action user code here
                switchDisplayable(null, getFrmDBList());//GEN-LINE:|7-commandAction|34|156-postAction
                // write post-action user code here
            } else if (command == itemCommand1) {//GEN-LINE:|7-commandAction|35|158-preAction
                // write pre-action user code here
                switchDisplayable(null, getFileBrowserExport());//GEN-LINE:|7-commandAction|36|158-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|37|54-preAction
        } else if (displayable == frmLogin) {
            if (command == createCommand) {//GEN-END:|7-commandAction|37|54-preAction
                // write pre-action user code here
                if(!getTxtPW().getString().equals(getTxtPW2().getString())) {
                    Alert alt = new Alert("Password error");
                    alt.setString("Passwords don't match.  Please check.");
                    switchDisplayable(alt, getFrmLogin());
                } else {
                    System.out.println("Generating key from: \"" + getTxtPW().getString() + "\"");
                    m_CipherParams = EncryptionUtils.createKey(getTxtPW().getString());
                    switchDisplayable(null, getFrmDBList());
                }
//GEN-LINE:|7-commandAction|38|54-postAction
                // write post-action user code here 
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|39|47-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|40|47-postAction
                // write post-action user code here
            } else if (command == loginCommand) {//GEN-LINE:|7-commandAction|41|19-preAction
                // write pre-action user code here
                m_CipherParams = EncryptionUtils.createKey(getTxtPW().getString());                
                if(loadRecords()) {
                    populateList();
                    switchDisplayable(null, getFrmDBList());
                } else {
                    errorMessage(m_Msg + "\nInvalid password (" + new String(Hex.encode(((KeyParameter) m_CipherParams).getKey())) + ") or database corrupted", getFrmLogin());
                }
//GEN-LINE:|7-commandAction|42|19-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|43|7-postCommandAction
        }//GEN-END:|7-commandAction|43|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|44|
//</editor-fold>//GEN-END:|7-commandAction|44|







//<editor-fold defaultstate="collapsed" desc=" Generated Getter: loginCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of loginCommand component.
     * @return the initialized component instance
     */
    public Command getLoginCommand() {
        if (loginCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            loginCommand = new Command("Login", Command.OK, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return loginCommand;
    }
//</editor-fold>//GEN-END:|18-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return itemCommand;
    }
//</editor-fold>//GEN-END:|21-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return okCommand1;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of screenCommand component.
     * @return the initialized component instance
     */
    public Command getScreenCommand() {
        if (screenCommand == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            screenCommand = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return screenCommand;
    }
//</editor-fold>//GEN-END:|25-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand1 ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of screenCommand1 component.
     * @return the initialized component instance
     */
    public Command getScreenCommand1() {
        if (screenCommand1 == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            screenCommand1 = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return screenCommand1;
    }
//</editor-fold>//GEN-END:|29-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmDBList ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of frmDBList component.
     * @return the initialized component instance
     */
    public List getFrmDBList() {
        if (frmDBList == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            frmDBList = new List("Password list", Choice.IMPLICIT);//GEN-BEGIN:|33-getter|1|33-postInit
            frmDBList.addCommand(getAddCommand());
            frmDBList.addCommand(getRemoveCommand());
            frmDBList.addCommand(getExportCommand());
            frmDBList.addCommand(getImportCommand());
            frmDBList.addCommand(getExitConfirmCommand());
            frmDBList.setCommandListener(this);
            frmDBList.setSelectedFlags(new boolean[]{});//GEN-END:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return frmDBList;
    }
//</editor-fold>//GEN-END:|33-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: frmDBListAction ">//GEN-BEGIN:|33-action|0|33-preAction
    /**
     * Performs an action assigned to the selected list element in the frmDBList component.
     */
    public void frmDBListAction() {//GEN-END:|33-action|0|33-preAction
        // enter pre-action user code here
        String __selectedString = getFrmDBList().getString(getFrmDBList().getSelectedIndex());//GEN-LINE:|33-action|1|33-postAction
        getPasswordForm().setPasswordEntry(((RecordEntry)m_Items.elementAt(getFrmDBList().getSelectedIndex())).item);
        getPasswordForm().show();        // enter post-action user code here
    }//GEN-BEGIN:|33-action|2|
//</editor-fold>//GEN-END:|33-action|2|




//<editor-fold defaultstate="collapsed" desc=" Generated Getter: addCommand ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initiliazed instance of addCommand component.
     * @return the initialized component instance
     */
    public Command getAddCommand() {
        if (addCommand == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            addCommand = new Command("Add", Command.ITEM, 0);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return addCommand;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|43-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|46-getter|0|46-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|46-getter|0|46-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|46-getter|1|46-postInit
            // write post-init user code here
        }//GEN-BEGIN:|46-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|46-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: altConfigError ">//GEN-BEGIN:|49-getter|0|49-preInit
    /**
     * Returns an initiliazed instance of altConfigError component.
     * @return the initialized component instance
     */
    public Alert getAltConfigError() {
        if (altConfigError == null) {//GEN-END:|49-getter|0|49-preInit
            // write pre-init user code here
            altConfigError = new Alert("Can\'t create password", "Can\'t create the password database.", null, AlertType.ERROR);//GEN-BEGIN:|49-getter|1|49-postInit
            altConfigError.addCommand(getOkCommand2());
            altConfigError.setCommandListener(this);
            altConfigError.setTimeout(Alert.FOREVER);//GEN-END:|49-getter|1|49-postInit
            // write post-init user code here
        }//GEN-BEGIN:|49-getter|2|
        return altConfigError;
    }
//</editor-fold>//GEN-END:|49-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of okCommand2 component.
     * @return the initialized component instance
     */
    public Command getOkCommand2() {
        if (okCommand2 == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            okCommand2 = new Command("Ok", Command.OK, 0);//GEN-LINE:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return okCommand2;
    }
//</editor-fold>//GEN-END:|50-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtPW2 ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of txtPW2 component.
     * @return the initialized component instance
     */
    public TextField getTxtPW2() {
        if (txtPW2 == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            txtPW2 = new TextField("Re-enter password:", null, 32, TextField.ANY | TextField.PASSWORD);//GEN-LINE:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return txtPW2;
    }
//</editor-fold>//GEN-END:|52-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: createCommand ">//GEN-BEGIN:|53-getter|0|53-preInit
    /**
     * Returns an initiliazed instance of createCommand component.
     * @return the initialized component instance
     */
    public Command getCreateCommand() {
        if (createCommand == null) {//GEN-END:|53-getter|0|53-preInit
            // write pre-init user code here
            createCommand = new Command("Create DB", Command.OK, 0);//GEN-LINE:|53-getter|1|53-postInit
            // write post-init user code here
        }//GEN-BEGIN:|53-getter|2|
        return createCommand;
    }
//</editor-fold>//GEN-END:|53-getter|2|







//<editor-fold defaultstate="collapsed" desc=" Generated Getter: removeCommand ">//GEN-BEGIN:|83-getter|0|83-preInit
    /**
     * Returns an initiliazed instance of removeCommand component.
     * @return the initialized component instance
     */
    public Command getRemoveCommand() {
        if (removeCommand == null) {//GEN-END:|83-getter|0|83-preInit
            // write pre-init user code here
            removeCommand = new Command("Remove", Command.ITEM, 0);//GEN-LINE:|83-getter|1|83-postInit
            // write post-init user code here
        }//GEN-BEGIN:|83-getter|2|
        return removeCommand;
    }
//</editor-fold>//GEN-END:|83-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: altConfirmExit ">//GEN-BEGIN:|87-getter|0|87-preInit
    /**
     * Returns an initiliazed instance of altConfirmExit component.
     * @return the initialized component instance
     */
    public Alert getAltConfirmExit() {
        if (altConfirmExit == null) {//GEN-END:|87-getter|0|87-preInit
            // write pre-init user code here
            altConfirmExit = new Alert("Confirm exit", "Exit?", null, null);//GEN-BEGIN:|87-getter|1|87-postInit
            altConfirmExit.addCommand(getCancelCommand());
            altConfirmExit.addCommand(getOkCommand());
            altConfirmExit.setCommandListener(this);
            altConfirmExit.setTimeout(Alert.FOREVER);//GEN-END:|87-getter|1|87-postInit
            // write post-init user code here
        }//GEN-BEGIN:|87-getter|2|
        return altConfirmExit;
    }
//</editor-fold>//GEN-END:|87-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitConfirmCommand ">//GEN-BEGIN:|85-getter|0|85-preInit
    /**
     * Returns an initiliazed instance of exitConfirmCommand component.
     * @return the initialized component instance
     */
    public Command getExitConfirmCommand() {
        if (exitConfirmCommand == null) {//GEN-END:|85-getter|0|85-preInit
            // write pre-init user code here
            exitConfirmCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|85-getter|1|85-postInit
            // write post-init user code here
        }//GEN-BEGIN:|85-getter|2|
        return exitConfirmCommand;
    }
//</editor-fold>//GEN-END:|85-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|88-getter|0|88-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|88-getter|0|88-preInit
            // write pre-init user code here
            cancelCommand = new Command("No", Command.CANCEL, 0);//GEN-LINE:|88-getter|1|88-postInit
            // write post-init user code here
        }//GEN-BEGIN:|88-getter|2|
        return cancelCommand;
    }
//</editor-fold>//GEN-END:|88-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|90-getter|0|90-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|90-getter|0|90-preInit
            // write pre-init user code here
            okCommand = new Command("Yes", Command.OK, 0);//GEN-LINE:|90-getter|1|90-postInit
            // write post-init user code here
        }//GEN-BEGIN:|90-getter|2|
        return okCommand;
    }
//</editor-fold>//GEN-END:|90-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Method: configLoad ">//GEN-BEGIN:|101-if|0|101-preIf
    /**
     * Performs an action assigned to the configLoad if-point.
     */
    public void configLoad() {//GEN-END:|101-if|0|101-preIf
        // enter pre-if user code here
        if (loadConfig()) {//GEN-LINE:|101-if|1|102-preAction
            // write pre-action user code here
            try {
                if(m_PasswordStore.getNumRecords() > 0) {
                    removeFormItem(getFrmLogin(), getTxtPW2());
                    getFrmLogin().removeCommand(getCreateCommand());
                } else {
                    getFrmLogin().removeCommand(getLoginCommand());
                }
            } catch(Exception e) {
                getFrmLogin().removeCommand(getLoginCommand());
                errorMessage("Error loading configuration: " + e.toString(), null);                
            }
            switchDisplayable(null, getFrmLogin());//GEN-LINE:|101-if|2|102-postAction
            // write post-action user code here
        } else {//GEN-LINE:|101-if|3|103-preAction
            // write pre-action user code here
            switchDisplayable(null, getAltConfigError());//GEN-LINE:|101-if|4|103-postAction
            // write post-action user code here
        }//GEN-LINE:|101-if|5|101-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|101-if|6|
//</editor-fold>//GEN-END:|101-if|6|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: altConfirmRemove ">//GEN-BEGIN:|107-getter|0|107-preInit
    /**
     * Returns an initiliazed instance of altConfirmRemove component.
     * @return the initialized component instance
     */
    public Alert getAltConfirmRemove() {
        if (altConfirmRemove == null) {//GEN-END:|107-getter|0|107-preInit
            // write pre-init user code here
            altConfirmRemove = new Alert("Remove item?", "Confirm item removal", null, null);//GEN-BEGIN:|107-getter|1|107-postInit
            altConfirmRemove.addCommand(getCancelRemoveCommand());
            altConfirmRemove.addCommand(getConfirmRemoveCommand());
            altConfirmRemove.setCommandListener(this);
            altConfirmRemove.setTimeout(Alert.FOREVER);//GEN-END:|107-getter|1|107-postInit
            // write post-init user code here
        }//GEN-BEGIN:|107-getter|2|
        return altConfirmRemove;
    }
//</editor-fold>//GEN-END:|107-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelRemoveCommand ">//GEN-BEGIN:|109-getter|0|109-preInit
    /**
     * Returns an initiliazed instance of cancelRemoveCommand component.
     * @return the initialized component instance
     */
    public Command getCancelRemoveCommand() {
        if (cancelRemoveCommand == null) {//GEN-END:|109-getter|0|109-preInit
            // write pre-init user code here
            cancelRemoveCommand = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|109-getter|1|109-postInit
            // write post-init user code here
        }//GEN-BEGIN:|109-getter|2|
        return cancelRemoveCommand;
    }
//</editor-fold>//GEN-END:|109-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: confirmRemoveCommand ">//GEN-BEGIN:|111-getter|0|111-preInit
    /**
     * Returns an initiliazed instance of confirmRemoveCommand component.
     * @return the initialized component instance
     */
    public Command getConfirmRemoveCommand() {
        if (confirmRemoveCommand == null) {//GEN-END:|111-getter|0|111-preInit
            // write pre-init user code here
            confirmRemoveCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|111-getter|1|111-postInit
            // write post-init user code here
        }//GEN-BEGIN:|111-getter|2|
        return confirmRemoveCommand;
    }
//</editor-fold>//GEN-END:|111-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelChooseFile ">//GEN-BEGIN:|118-getter|0|118-preInit
    /**
     * Returns an initiliazed instance of cancelChooseFile component.
     * @return the initialized component instance
     */
    public Command getCancelChooseFile() {
        if (cancelChooseFile == null) {//GEN-END:|118-getter|0|118-preInit
            // write pre-init user code here
            cancelChooseFile = new Command("Back", Command.BACK, 0);//GEN-LINE:|118-getter|1|118-postInit
            // write post-init user code here
        }//GEN-BEGIN:|118-getter|2|
        return cancelChooseFile;
    }
//</editor-fold>//GEN-END:|118-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: selectFileCommand ">//GEN-BEGIN:|120-getter|0|120-preInit
    /**
     * Returns an initiliazed instance of selectFileCommand component.
     * @return the initialized component instance
     */
    public Command getSelectFileCommand() {
        if (selectFileCommand == null) {//GEN-END:|120-getter|0|120-preInit
            // write pre-init user code here
            selectFileCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|120-getter|1|120-postInit
            // write post-init user code here
        }//GEN-BEGIN:|120-getter|2|
        return selectFileCommand;
    }
//</editor-fold>//GEN-END:|120-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exportCommand ">//GEN-BEGIN:|122-getter|0|122-preInit
    /**
     * Returns an initiliazed instance of exportCommand component.
     * @return the initialized component instance
     */
    public Command getExportCommand() {
        if (exportCommand == null) {//GEN-END:|122-getter|0|122-preInit
            // write pre-init user code here
            exportCommand = new Command("Export", Command.ITEM, 0);//GEN-LINE:|122-getter|1|122-postInit
            // write post-init user code here
        }//GEN-BEGIN:|122-getter|2|
        return exportCommand;
    }
//</editor-fold>//GEN-END:|122-getter|2|









//<editor-fold defaultstate="collapsed" desc=" Generated Getter: frmGetFileName ">//GEN-BEGIN:|154-getter|0|154-preInit
    /**
     * Returns an initiliazed instance of frmGetFileName component.
     * @return the initialized component instance
     */
    public Form getFrmGetFileName() {
        if (frmGetFileName == null) {//GEN-END:|154-getter|0|154-preInit
            // write pre-init user code here
            frmGetFileName = new Form("Export data", new Item[]{getTxtSaveFilename()});//GEN-BEGIN:|154-getter|1|154-postInit
            frmGetFileName.addCommand(getBackCommand2());
            frmGetFileName.addCommand(getItemCommand1());
            frmGetFileName.setCommandListener(this);//GEN-END:|154-getter|1|154-postInit
            // write post-init user code here
        }//GEN-BEGIN:|154-getter|2|
        return frmGetFileName;
    }
//</editor-fold>//GEN-END:|154-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: txtSaveFilename ">//GEN-BEGIN:|159-getter|0|159-preInit
    /**
     * Returns an initiliazed instance of txtSaveFilename component.
     * @return the initialized component instance
     */
    public TextField getTxtSaveFilename() {
        if (txtSaveFilename == null) {//GEN-END:|159-getter|0|159-preInit
            // write pre-init user code here
            txtSaveFilename = new TextField("Filename to save as:", null, 32, TextField.ANY);//GEN-LINE:|159-getter|1|159-postInit
            // write post-init user code here
        }//GEN-BEGIN:|159-getter|2|
        return txtSaveFilename;
    }
//</editor-fold>//GEN-END:|159-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|149-getter|0|149-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|149-getter|0|149-preInit
            // write pre-init user code here
            backCommand1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|149-getter|1|149-postInit
            // write post-init user code here
        }//GEN-BEGIN:|149-getter|2|
        return backCommand1;
    }
//</editor-fold>//GEN-END:|149-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exportToDirCommand ">//GEN-BEGIN:|151-getter|0|151-preInit
    /**
     * Returns an initiliazed instance of exportToDirCommand component.
     * @return the initialized component instance
     */
    public Command getExportToDirCommand() {
        if (exportToDirCommand == null) {//GEN-END:|151-getter|0|151-preInit
            // write pre-init user code here
            exportToDirCommand = new Command("Export", Command.ITEM, 0);//GEN-LINE:|151-getter|1|151-postInit
            // write post-init user code here
        }//GEN-BEGIN:|151-getter|2|
        return exportToDirCommand;
    }
//</editor-fold>//GEN-END:|151-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|155-getter|0|155-preInit
    /**
     * Returns an initiliazed instance of backCommand2 component.
     * @return the initialized component instance
     */
    public Command getBackCommand2() {
        if (backCommand2 == null) {//GEN-END:|155-getter|0|155-preInit
            // write pre-init user code here
            backCommand2 = new Command("Back", Command.BACK, 0);//GEN-LINE:|155-getter|1|155-postInit
            // write post-init user code here
        }//GEN-BEGIN:|155-getter|2|
        return backCommand2;
    }
//</editor-fold>//GEN-END:|155-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand1 ">//GEN-BEGIN:|157-getter|0|157-preInit
    /**
     * Returns an initiliazed instance of itemCommand1 component.
     * @return the initialized component instance
     */
    public Command getItemCommand1() {
        if (itemCommand1 == null) {//GEN-END:|157-getter|0|157-preInit
            // write pre-init user code here
            itemCommand1 = new Command("Choose Folder", Command.ITEM, 0);//GEN-LINE:|157-getter|1|157-postInit
            // write post-init user code here
        }//GEN-BEGIN:|157-getter|2|
        return itemCommand1;
    }
//</editor-fold>//GEN-END:|157-getter|2|





//<editor-fold defaultstate="collapsed" desc=" Generated Getter: importCommand ">//GEN-BEGIN:|164-getter|0|164-preInit
    /**
     * Returns an initiliazed instance of importCommand component.
     * @return the initialized component instance
     */
    public Command getImportCommand() {
        if (importCommand == null) {//GEN-END:|164-getter|0|164-preInit
            // write pre-init user code here
            importCommand = new Command("Import", Command.ITEM, 0);//GEN-LINE:|164-getter|1|164-postInit
            // write post-init user code here
        }//GEN-BEGIN:|164-getter|2|
        return importCommand;
    }
//</editor-fold>//GEN-END:|164-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand3 ">//GEN-BEGIN:|170-getter|0|170-preInit
    /**
     * Returns an initiliazed instance of backCommand3 component.
     * @return the initialized component instance
     */
    public Command getBackCommand3() {
        if (backCommand3 == null) {//GEN-END:|170-getter|0|170-preInit
            // write pre-init user code here
            backCommand3 = new Command("Back", Command.BACK, 0);//GEN-LINE:|170-getter|1|170-postInit
            // write post-init user code here
        }//GEN-BEGIN:|170-getter|2|
        return backCommand3;
    }
//</editor-fold>//GEN-END:|170-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand2 ">//GEN-BEGIN:|173-getter|0|173-preInit
    /**
     * Returns an initiliazed instance of itemCommand2 component.
     * @return the initialized component instance
     */
    public Command getItemCommand2() {
        if (itemCommand2 == null) {//GEN-END:|173-getter|0|173-preInit
            // write pre-init user code here
            itemCommand2 = new Command("Item", Command.ITEM, 0);//GEN-LINE:|173-getter|1|173-postInit
            // write post-init user code here
        }//GEN-BEGIN:|173-getter|2|
        return itemCommand2;
    }
//</editor-fold>//GEN-END:|173-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: fileBrowserExport ">//GEN-BEGIN:|174-getter|0|174-preInit
    /**
     * Returns an initiliazed instance of fileBrowserExport component.
     * @return the initialized component instance
     */
    public FileBrowserExtended getFileBrowserExport() {
        if (fileBrowserExport == null) {//GEN-END:|174-getter|0|174-preInit
            // write pre-init user code here
            fileBrowserExport = new FileBrowserExtended();//GEN-BEGIN:|174-getter|1|174-postInit
            fileBrowserExport.setTitle("Choose export folder");
            fileBrowserExport.addCommand(getBackCommand1());
            fileBrowserExport.addCommand(getExportToDirCommand());
            fileBrowserExport.setCommandListener(this);//GEN-END:|174-getter|1|174-postInit
            // write post-init user code here
        }//GEN-BEGIN:|174-getter|2|
        return fileBrowserExport;
    }
//</editor-fold>//GEN-END:|174-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: fileBrowserExportAction ">//GEN-BEGIN:|174-action|0|174-preAction
    /**
     * Performs an action assigned to the selected list element in the fileBrowserExport component.
     */
    public void fileBrowserExportAction() {//GEN-END:|174-action|0|174-preAction
        // enter pre-action user code here
        String __selectedString = getFileBrowserExport().getString(getFileBrowserExport().getSelectedIndex());//GEN-LINE:|174-action|1|174-postAction
        // enter post-action user code here
        if(exportFileAction(getFileBrowserExport().getSelectedFile().getURL())) {
            switchDisplayable(null, getFrmDBList());
        } else {
            errorMessage(m_Msg, null);
        }
    }//GEN-BEGIN:|174-action|2|
//</editor-fold>//GEN-END:|174-action|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: fileBrowserImport ">//GEN-BEGIN:|183-getter|0|183-preInit
    /**
     * Returns an initiliazed instance of fileBrowserImport component.
     * @return the initialized component instance
     */
    public FileBrowserExtended getFileBrowserImport() {
        if (fileBrowserImport == null) {//GEN-END:|183-getter|0|183-preInit
            // write pre-init user code here
            fileBrowserImport = new FileBrowserExtended();//GEN-BEGIN:|183-getter|1|183-postInit
            fileBrowserImport.setTitle("Import password file");
            fileBrowserImport.addCommand(getBackCommand());
            fileBrowserImport.setCommandListener(this);//GEN-END:|183-getter|1|183-postInit
            // write post-init user code here
        }//GEN-BEGIN:|183-getter|2|
        return fileBrowserImport;
    }
//</editor-fold>//GEN-END:|183-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: fileBrowserImportAction ">//GEN-BEGIN:|183-action|0|183-preAction
    /**
     * Performs an action assigned to the selected list element in the fileBrowserImport component.
     */
    public void fileBrowserImportAction() {//GEN-END:|183-action|0|183-preAction
        // enter pre-action user code here
        String __selectedString = getFileBrowserImport().getString(getFileBrowserImport().getSelectedIndex());//GEN-LINE:|183-action|1|183-postAction
        // enter post-action user code here
        if(importFileAction(getFileBrowserImport().getSelectedFile().getURL())) {
            switchDisplayable(null, getFrmDBList());
        } else {
            errorMessage(m_Msg, null);
        }
    }//GEN-BEGIN:|183-action|2|
//</editor-fold>//GEN-END:|183-action|2|















    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {        
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    private void removeFormItem(Form frm, Item item) {
        int i = 0;
        while(i < frm.size()) {
            if(item == frm.get(i)) {
                frm.delete(i);
                i = frm.size();
            } else {
                i++;
            }
        }
    }

    public void notifyFileSelected(FileConnection fc) {
        System.out.println("file chosen: "+ fc.getURL());
    }

    public void notifySaveSelected(FileConnection fc) {
        System.out.println("file to save: " + fc.getURL());
    }
    class RecordVector extends Vector {
        /**
         * Inserts item into an already sorted list
         * @param item
         * @return index that the item was inserted at
         */
            public int insert(Comparable item) {
                Comparable elt;
                System.out.println("RecordVector::insert(" + item.toString() + ")");
                if(this.size() > 0) {
                    elt = (Comparable) this.elementAt(0);
                    System.out.println("compare with first element: " + elt.compareTo(item));
                }
                if(this.size() > 1) {
                    elt = (Comparable) this.elementAt(this.size()-1);
                    System.out.println("compare with last element: " + elt.compareTo(item));                    
                }
                if(this.size() == 0) {
                    this.addElement(item);
                    return 0;
                } else if(this.size() == 1) {
                    if(((Comparable)this.elementAt(0)).compareTo(item) <= 0) {
                        System.out.println("Appending");
                        this.addElement(item);
                        return 1;
                    } else {
                        System.out.println("Prepending");
                        this.insertElementAt(item, 0);
                        return 0;
                    }
                } else if(((Comparable)this.elementAt(0)).compareTo(item) >= 0) {
                    System.out.println("Prepending");
                    this.insertElementAt(item, 0);
                    return 0;
                } else if(((Comparable)this.elementAt(this.size()-1)).compareTo(item) <= 0) {
                    System.out.println("Appending");
                    this.addElement(item);
                    return this.size()-1;
                } else {
                    int l = 0, t = this.size()-1;
                    int m = (l+t)/2;                
                    while(l + 1 < t) {
                        elt = (Comparable) this.elementAt(m);
                        System.out.println("Comparison with " + elt.toString() + ": " + elt.compareTo(item));
                        if((((Comparable)this.elementAt(m)).compareTo(item)) < 0) {
                            l = m;
                        } else {
                            t = m;
                        }
                        m = (l+t)/2;
                    }
                    System.out.println("Inserting at " + t);
                    this.insertElementAt(item, t);
                    return t;
            }
        }
    }
    class RecordEntry implements Comparable {
        int id;
        PasswordEntry item;
        public String toString() {
            return "RecordEntry(" + id + ", " + item.toString() + ")";
        }
        public RecordEntry(int id, PasswordEntry item) {
             this.id = id;
             this.item = item;
         }

        public int compareTo(Object o) {
            RecordEntry rhs = (RecordEntry) o;
            return item.compareTo(rhs.item);
        }
    }
    private boolean loadConfig() {
        try {
            m_PasswordStore = RecordStore.openRecordStore("Passwords", false);
            return true;
        } catch(Exception e) {
            try {
                m_PasswordStore = RecordStore.openRecordStore("Passwords", true);
                return true;
            } catch(Exception e2) {
                //can't create or open the password store: display error msgbox
                return false;
            }
        }
    }
    private boolean loadRecords() {
        class RecordCompare implements RecordComparator {
            private CipherParameters m_Param;
            public RecordCompare(MainMenu enc, CipherParameters m_Params) {
                this.m_Param  =m_Params;
            }
            public int compare(byte[] rec1, byte[] rec2) {
                PasswordEntry rhs = new PasswordEntry(), lhs = new PasswordEntry();
                
                rhs.fromBytes(EncryptionUtils.performDecrypt(m_Param, rec1));
                lhs.fromBytes(EncryptionUtils.performDecrypt(m_Param, rec2));
                return rhs.compareTo(lhs);
            }

        }
        try {
            //make sure the password is valid
            byte[] data = m_PasswordStore.enumerateRecords(null, null, false).nextRecord();
            data = EncryptionUtils.performDecrypt(m_CipherParams, data);
            if(data == null) {
                return false;
            } else {
                RecordEnumeration rEnum = m_PasswordStore.enumerateRecords(null, null, false);
                for(;rEnum.hasNextElement();) {
                    int idx = rEnum.nextRecordId();
                    data = EncryptionUtils.performDecrypt(m_CipherParams, m_PasswordStore.getRecord(idx));
                    PasswordEntry ent = new PasswordEntry();
                    ent.fromBytes(data);
                    m_Items.insert(new RecordEntry(idx, ent));                    
                }
            }
            
            return true;
        } catch(Exception e) {
            m_Msg = "Exception in accessing record: " + e.toString();
            return false;
        }
    }
    private void notImplemented() {
        Alert alt = new Alert("Not implemented");
        alt.setString("Not built yet");
        switchDisplayable(alt, getFrmLogin());
    }
    private PasswordEntryForm getPasswordForm() {
        if(m_PasswordForm == null) {
            m_PasswordForm = new PasswordEntryForm(this);
        }
        return m_PasswordForm;
    }
    public void notifyUpdate(PasswordEntry ent) {        
        byte[] dest = ent.toBytes();
        byte[] res = EncryptionUtils.performEncrypt(m_CipherParams, dest);
        if(res == null) {
            errorMessage("Error in encrypting", getFrmDBList());
        } else {
            try {
                RecordEntry rec = null;
                if(getFrmDBList().getSelectedIndex() != -1) {
                    rec = (RecordEntry) m_Items.elementAt(getFrmDBList().getSelectedIndex());
                }
                int idx;
                if(rec != null && rec.item == ent) {
                    idx = rec.id;
                    m_Items.removeElementAt(getFrmDBList().getSelectedIndex());
                    m_PasswordStore.setRecord(idx, res, 0, res.length);
                } else {
                    idx = m_PasswordStore.addRecord(res, 0, res.length);
                    rec = new RecordEntry(idx, ent);
                }
                idx = m_Items.insert(rec);
                populateList();
                getFrmDBList().setSelectedIndex(idx, true);
                switchDisplayable(null, getFrmDBList());
            } catch(Exception e) {
                errorMessage("Unable to save record: " + e.toString(), getFrmDBList());
            }
        }
    }
    private void populateList() {
        getFrmDBList().deleteAll();
        for(Enumeration e = m_Items.elements(); e.hasMoreElements(); ) {
            PasswordEntry item = ((RecordEntry) e.nextElement()).item;
            getFrmDBList().append(item.Description, null);
        }        
    }
    private void errorMessage(String msg, Displayable next) {
        if(next == null) next = getDisplay().getCurrent();
        Alert alt = new Alert("Error");
        alt.setString(msg);
        alt.setTimeout(Alert.FOREVER);
        switchDisplayable(alt, next);
    }
    private boolean importFileAction(String fn) {
        try {
            FileConnection fc = (FileConnection) Connector.open(fn); 
            InputStream is = fc.openInputStream();
            byte[] data = new byte[(int)fc.fileSize()];
            is.read(data);
            data = EncryptionUtils.performDecrypt(m_CipherParams, data);
            if(data == null) {
                m_Msg = "Error decrypting";
                return false;
            } else {
                Vector res = DataSerializer.getVector(data);
                for(Enumeration elt = res.elements(); elt.hasMoreElements();) {
                    PasswordEntry e = (PasswordEntry) elt.nextElement();
                    byte[] rec = EncryptionUtils.performEncrypt(m_CipherParams, e.toBytes());
                    int idx = m_PasswordStore.addRecord(rec, 0, rec.length);
                    m_Items.insert(new RecordEntry(idx, e));
                }
                populateList();
            }
            return true;
        } catch(Exception e) {
            m_Msg = "Error importing from " + fn + ":" + e.toString();
            return false;
        }        
    }
    private boolean exportFileAction(String fn) {
        try {
            FileConnection fc = (FileConnection) Connector.open(fn);
            if(!fc.exists()) fc.create();
            OutputStream os = fc.openOutputStream();
            os.write(
                    EncryptionUtils.performEncrypt(
                        m_CipherParams, 
                        DataSerializer.getBytes(m_Items.elements())
                    )
            );
            os.close();
            return true;
        } catch(Exception e) {
            m_Msg = "Error exporting to " + fn + ": " + e.toString();
            return false;
        }
    }
}
